from django.shortcuts import render_to_response
from mysite.books.models import Book



def search_form(request):
	return render_to_response('search_form.html')
	
def search(request):
	errors = []
	if 'q' in request.GET:
		q = request.GET['q']
		if not q:
			errors.append('No data')
		elif len(q) > 20:
			errors.append('Too much data')
		else:
			books = Book.objects.filter(title__icontains=q)
			return render_to_response('search_results.html',
				{'books': books, 'query': q})
	return render_to_response('search_form.html',
		{'errors': errors})
		

